function [newZ] = onFlyDelete(startIdx,curTime,endIdx,prevZ, sites, triangles, gridStructure)
% delete site next site index

curArc = prevZ(startIdx:startIdx+2,1:3); % arc that the user is currently on
finalArc = prevZ(endIdx-2:endIdx,1:3); % arc that enters the site after deleted site
t = curTime/10; % t=[0~1]

startPoint = ((1-t)^2)*curArc(1,:) + 2*(1-t)*t*curArc(2,:) + (t^2)*curArc(3,:);
endPoint = prevZ(endIdx,1:3);


startSlope = -2*(1-t)*curArc(1,:) + 2*(1-2*t)*curArc(2,:) + 2*t*curArc(3,:);  % Fstart'(t)
endSlope = -2*finalArc(2,:) + 2*finalArc(3,:);% Fend'(1)

% interpolate points inbetween. try 2
P = [startPoint; endPoint];
[P origIndex newIndex] = interpolateP(P, sites);

% optimize route conditioned on C1 continuity at end points
checkArcs = []; fixedIndex = [];
N = 3; iteration = 10;
eps = 0.0001; delta = 20; margin = 10;
paramDist = 0.2; paramCurve = 0.8; numAddPoints = 1;

for iter=1:iteration
    initialize_cvx;
    disp(['Begin cvx iteration : ' num2str(iter)]);

    cvx_begin
    variables w(m) x(m) y(m) z(m) dx(n) dy(n) dz(n);

    % declare variables used in cvx
    approxP = P + [dx dy dz];
    Z = []; clear C;
    for k=1:n-1
       C(k,:) = [x((k-1)*N + 1) y((k-1)*N + 1) z((k-1)*N + 1)];
       Z = [Z; approxP(k,:); C(k,:)];
    end
    Z = [Z; approxP(n,:)];

    % optimize function
    minimize( paramDist*sum(w) + paramCurve*norm(T2*Z,'fro'));

    subject to
        % Cone Form
        for k=1:m
            ind = int8(floor(((k-1)/N)+eps)+1);

            A = A1(k)-A2(k);
            B = B11(k)*approxP(ind,:) + B12(k)*approxP(ind+1,:) - B21(k)*approxP(ind,:) - B22(k)*approxP(ind+1,:);

            norm(A*[x(k) y(k) z(k)] + B) <= w(k);

        end

        % C1 continuity
        for k=1:(m/N)-1
            ind = (k-1)*N + 1;
            approxP(k+1,:) == (1/2)*([x(ind) y(ind) z(ind)] + [x(ind+N) y(ind+N) z(ind+N)]); 
        end
        % C1 continuity at start & end
        -2*Z(1,:) + 2*Z(2,:) == startSlope;
        -2*Z(2*(n-1),:) + 2*Z(2*(n-1)+1,:) == endSlope;

        % constraint that C1=C2=...=CN
        T*[x y z] == zeros(m,3);  

        % constraint on delta
        if(~isempty(newIndex))
            dz(newIndex) >= zeros(length(newIndex),1);    
        end
        abs([dx(origIndex) dy(origIndex) dz(origIndex)]) <= delta*ones(length(origIndex),3);
        [dx(1) dy(1) dz(1)] == zeros(1,3);
        [dx(n) dy(n) dz(n)] == zeros(1,3);
        
        z >= zeros(m,1);

%         % arcs that should be preserved from previous optimization
%         for i=1:length(fixedIndex)
%             ind = (fixedIndex(i)-1)*N + 1;
% %                 [x(ind) y(ind) z(ind)]) == fixedOffPoints(i,:);
%             abs([x(ind) y(ind) z(ind)] - fixedOffPoints(i,:)) <= margin;
%         end


    cvx_end
    cvx_status

    % check results
%     if(iter==1) 
        checkArcs = 1:n-1; 
%     end;
    [checkArcs newP origIndex newIndex fixedIndex fixedOffPoints] = checkResult(x,y,z,P,approxP,origIndex,sites,triangles, checkArcs, n, N,gridStructure, numAddPoints);

    % visualization
    [C Z] = visualizeCurve(x,y,z,P,approxP,n,N,origIndex);

    % update P and re-run cvx
    if(length(newP)==length(P) || iter == iteration) break;
    else
        disp([ num2str(length(newP)-length(P)) ' new nodes added']);
        P = newP;
    end
end

% if begining position is on site modify
if(prevZ(startIdx,4)==1 && curTime ==0)
    Z(1,4) = 1;
else
    Z(1,4) = 0;
end

newZ = Z; % just send newly added points
% newZ = [Z; prevZ(endIdx+1:length(prevZ),:)]; % send all points onwards

save('../data/finalData/delete.sequence','newZ','-ASCII', '-tabs')
disp('New Trajectory for OnFlyDelete saved');